package com.ecy.quadratic.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.ecy.quadratic.R;
import com.ecy.quadratic.adapter.CommunityListViewAdapter;
import com.ecy.quadratic.beans.Community;
import com.ecy.quadratic.beans.CommunitySpecialArea;
import com.ecy.quadratic.beans.PageInfo;
import com.ecy.quadratic.beans.support.CommentNumEvent;
import com.ecy.quadratic.beans.support.CommunityEvent;
import com.ecy.quadratic.component.DaggerCommunityMainComponent;
import com.ecy.quadratic.reader.base.BaseActivity;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.ui.contract.LastCommunityContract;
import com.ecy.quadratic.ui.presenter.LastCommunityPresenter;
import com.ecy.quadratic.widget.SwipeRefreshView;
import com.ecy.quadratic.widget.SwipeRefreshView.OnLoadListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;


/**
 * Created by Administrator on 2016/3/31.
 */
public class SpecialAreaActivity extends BaseActivity implements LastCommunityContract.View, CommunityListViewAdapter.LikeListener {

    public static final String SPECIAL_AREA = "specialarea";
    @Bind(R.id.community_hot_swipe_refreshview)
    SwipeRefreshView mSwipeRefreshView;
    @Bind(R.id.community_hot_listview)
    ListView mComicListView;

    @Inject
    LastCommunityPresenter mPresenter;

    private CommunityListViewAdapter mAdapter;
    private List<Community> mCommunityList;
    private int mCurrentPage = 0;
    private int mTotalPage = 0;
    private CommunitySpecialArea mSpecialArea;
    private OnRefreshListener mOnRefreshListener = new OnRefreshListener() {

        @Override
        public void onRefresh() {
            mPresenter.refresh("1");
        }
    };
    private OnLoadListener mOnLoadListener = new OnLoadListener() {

        @Override
        public void onLoad() {
            mPresenter.loadMore(mCurrentPage + 1 + "");
        }
    };

    public SpecialAreaActivity() {
    }

    public static void startActivity(Context context, CommunitySpecialArea specialArea) {
        Intent intent = new Intent(context, SpecialAreaActivity.class);
        intent.putExtra(SPECIAL_AREA, specialArea);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_specialarea_community;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerCommunityMainComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void initToolBar() {

    }

    @Override
    public void initData() {
        mSpecialArea = (CommunitySpecialArea) getIntent().getSerializableExtra(SPECIAL_AREA);
        mCommunityList = new ArrayList<Community>();
        mAdapter = new CommunityListViewAdapter(this, mCommunityList);
    }

    @Override
    public void configViews() {

        mSwipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSwipeRefreshView.setColorSchemeResources(R.color.hobby_theme_color, R.color.hobby_theme_color, R.color.hobby_theme_color);
        mSwipeRefreshView.setOnRefreshListener(mOnRefreshListener);
        mSwipeRefreshView.setOnLoadListener(mOnLoadListener);
        View view = LayoutInflater.from(this).inflate(R.layout.specialarea_header, null);
        ImageView coverIV = (ImageView) view.findViewById(R.id.special_cover);
        Glide.with(this).load(mSpecialArea.getImgResId()).into(coverIV);
        mComicListView.addHeaderView(view);
        mComicListView.setAdapter(mAdapter);
        mSwipeRefreshView.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshView.setRefreshing(true);
                mOnRefreshListener.onRefresh();
            }
        });
        mAdapter.setLikeListener(this);
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showRefreshList(PageInfo<Community> pageInfo) {

        if (pageInfo != null) {
            mCurrentPage = pageInfo.pageNum;
            mTotalPage = pageInfo.pages;
            mCommunityList.clear();
            mCommunityList.addAll(pageInfo.list);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showLoadMoreList(PageInfo<Community> pageInfo) {
        if (pageInfo != null) {
            mCurrentPage = pageInfo.pageNum;
            mTotalPage = pageInfo.pages;
            mCommunityList.addAll(pageInfo.list);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void postLikeSuccessful() {

    }

    @Override
    public void showError() {
        mSwipeRefreshView.setRefreshing(false);
        mSwipeRefreshView.setLoading(false, true);
    }

    @Override
    public void complete() {
        mSwipeRefreshView.setRefreshing(false);
        mSwipeRefreshView.setLoading(false);
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void refresh(CommunityEvent event) {
        mSwipeRefreshView.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshView.setRefreshing(true);
                mOnRefreshListener.onRefresh();
            }
        });
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void updateNum(CommentNumEvent event) {
        mAdapter.updateCommentNum(event.mCommunityId);
    }

    @Override
    public void onLike(String id) {
        mPresenter.postLike(id);
    }
}
